package ${basepackage}.serviceimpl.${module};

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.masget.command.global.SessionManager;
import com.masget.command.global.tf_session;
import com.masget.command.utils.BeanMapUtils;
import com.masget.command.utils.CheckJSONDataUtil;
import com.masget.command.utils.JSONUtils;
import com.masget.command.utils.QueryWrapperUtils;
import com.masget.entity.RetStruct;
import com.masget.utils.MasgetLogger;
import com.masget.utils.MgException;
import ${basepackage}.service.${module}.${table.className}Service;
import ${basepackage}.entity.${module}.${table.className};

/**
 * <b>function:</b> ${table.remarks!className}服务API
 * @author ${author}
 * @createDate <#if now??>${now?string('yyyy-MM-dd HH:mm:ss')}</#if>
 * @file ${table.className}.java
 * @package ${basepackage}.serviceimpl.${module}
 * @project ${app}
 * @blog ${blog}
 * @email ${email}
 * @version 1.0
 */
@Component
public class ${table.className}ServiceImpl {

	@Autowired
	private ${table.className}Service<${table.className}> service;
	
	/**
	 * 添加${table.remarks!className}
	 * <masget.discount.>
	 * @author hoojo
	 * @createDate <#if now??>${now?string('yyyy-MM-dd HH:mm:ss')}</#if>
	 */
	public String add(String data, String sessionid) throws Exception {
		tf_session session = SessionManager.getInstance().getSession(sessionid);
		
		JSONObject params = (JSONObject) CheckJSONDataUtil.checkJSONData(data);
		
		try {
			${table.className} entity = new ${table.className}();
			BeanMapUtils.transMap2Bean(params, entity);
			
			if (service.add(entity)) {
				return new RetStruct("批量添加${table.remarks!className}成功").toString();
			}
			return  new RetStruct(10001, "批量添加${table.remarks!className}失败").toString();
		} catch (Exception e) {
			MasgetLogger.error("批量添加${table.remarks!className}异常", e);
			throw e;
		}
	}
	
	/**
	 * 修改${table.remarks!className}
	 * <masget.discount.>
	 * @author hoojo
	 * @createDate <#if now??>${now?string('yyyy-MM-dd HH:mm:ss')}</#if>
	 */
	public String edit(String data, String sessionid) throws Exception {
		tf_session session = SessionManager.getInstance().getSession(sessionid);
		
		JSONObject params = (JSONObject) CheckJSONDataUtil.checkJSONData(data);
		
		try {
			${table.className} entity = new ${table.className}();
			BeanMapUtils.transMap2Bean(params, entity);
			
			entity.setUpdater(session.getStaffid());

			if (service.edit(entity)) {
				return new RetStruct("修改${table.remarks!className}成功").toString();
			}
			return  new RetStruct(10001, "修改${table.remarks!className}失败").toString();
		} catch (Exception e) {
			MasgetLogger.error("修改${table.remarks!className}异常", e);
			throw e;
		}
	}
	
	/**
	 * 删除${table.remarks!className}
	 * <masget.discount.>
	 * @author hoojo
	 * @createDate <#if now??>${now?string('yyyy-MM-dd HH:mm:ss')}</#if>
	 */
	public String remove(String data, String sessionid) throws Exception {
		SessionManager.getInstance().getSession(sessionid);
		
		try {
			${table.className} entity = CheckJSONDataUtil.checkJSONData(data, ${table.className}.class);
			
			if (service.remove(entity)) {
				return new RetStruct("删除${table.remarks!className}成功").toString();
			}
			return  new RetStruct(10001, "删除${table.remarks!className}失败").toString();
		} catch (Exception e) {
			MasgetLogger.error("删除${table.remarks!className}异常", e);
			throw e;
		}
	}
	
	/**
	 * 查询分页${table.remarks!className}
	 * <masget.discount.>
	 * @author hoojo
	 * @createDate <#if now??>${now?string('yyyy-MM-dd HH:mm:ss')}</#if>
	 */
	public String query(String data, String sessionid) throws Exception {
		SessionManager.getInstance().getSession(sessionid);
		
		JSONObject params = (JSONObject) CheckJSONDataUtil.checkJSONData(data);
		try {
			${table.className} entity = new ${table.className}();
			BeanMapUtils.transMap2Bean(params, entity);
			
			JSONObject result = new JSONObject();
			result.put("rows", service.query(entity, QueryWrapperUtils.builder(params)));
			result.put("total", service.count(entity));
			
			return JSONUtils.toJson(new RetStruct("查询完成", "data", result));
		} catch (Exception e) {
			MasgetLogger.error("查询${table.remarks!className}数据异常", e);
			throw e;
		}
	}
}
